package com.lxs.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lxs.common.utils.PageUtils;
import com.lxs.common.utils.R;
import com.lxs.mall.product.entity.AttrEntity;
import com.lxs.mall.product.service.AttrAttrgroupRelationService;
import com.lxs.mall.product.service.AttrGroupService;
import com.lxs.mall.product.service.AttrService;
import com.lxs.mall.product.service.CategoryService;
import com.lxs.mall.product.vo.AttrGroupRelationVO;
import com.lxs.mall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lxs.mall.product.entity.AttrGroupEntity;


/**
 * 属性分组
 *
 * @author lxs
 * @email 1404637866@qq.com
 * @date 2023-05-11 22:27:17
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController{
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService relationService;
    /**
     * Url:/product/attrgroup/225/withattr?t=1686644926356
     *发布商品，下一步规格参数
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrgroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        // 根据的编号获取对应的属性组和属性组的属性信息
        List<AttrGroupWithAttrsVo> list = attrGroupService
                .getAttrgroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",list);
    }
    /**
     *url:/product/attrgroup/attr/relation
     * 批量保存关联属性信息
     */
    @PostMapping("/attr/relation")
    public R saveBatch(@RequestBody List<AttrGroupRelationVO> vos){
        relationService.saveBatch(vos);
        return R.ok();
    }
    /**
     * url :/product/attrgroup/2/noattr/relation?t=1686616829577&page=1&limit=10&key=
     * 查询出未关联的属性信息
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoRelationAttr(@PathVariable("attrgroupId") Long attrgroupId, @RequestParam Map<String,Object> params){
        PageUtils pageUtils = attrService.getNoAttrRelation(params,attrgroupId);
        return R.ok().put("page",pageUtils);
    }
    ///product/attrgroup/5/attr/relation?t=1686560925590
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> list = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", list);
    }
    /**
     * 列表
     * 分页参数：
     * {    page：当前页码，
     *      limit:每页显示记录数,
     *      sidx:排序字段，
     *      order：排序方式，
     *      key:查询关键字}
     *
     */
    @RequestMapping("/list/{catId}")
    public R listGroup(@RequestParam Map<String, Object> params,@PathVariable("catId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }
//    @RequestMapping("/list")
//    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = attrGroupService.queryPage(params);
//
//        return R.ok().put("page", page);
//    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		//根据属性组找到的对应的分类Id然后在找对对应的【一级，二级，三级】数据
        Long catelogId = attrGroup.getCatelogId();
        Long[] paths = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatalogPath(paths);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }
    /**
     * 批量删除
     * product/attrgroup/attr/relation/delete
     *
     */
    @PostMapping("/attr/relation/delete")
    public R relationDelete(@RequestBody AttrGroupRelationVO[] vos){
        attrService.relationDelete(vos);
        return R.ok();
    }
    /**
     *
     * 删除
     */

    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
